package com.itheima.controller;

import com.itheima.domain.annotation.LogOperation;
import com.itheima.domain.pojo.Clazz;
import com.itheima.domain.pojo.PageResult;
import com.itheima.domain.query.ClazzQueryParam;
import com.itheima.domain.vo.Result;
import com.itheima.mapper.ClazzMapper;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: Sch
 * @Date: 2025/07/10 18:19
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {
    @Autowired
    private ClazzService clazzService;
    @Autowired
    private ClazzMapper clazzMapper;

    /**
     * 新增班级
     *
     * @param
     * @return
     */
    @LogOperation
    @PostMapping
    public Result add(@RequestBody Clazz clazz) {
        clazzService.add(clazz);
        return Result.success();
    }

    /**
     * 根据id查询班级信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Clazz clazz = clazzService.getById(id);
        return Result.success(clazz);
    }


    /**
     * 查询所有班级信息
     *
     * @return
     */
    @GetMapping("/list")
    public Result findAll() {
        List<Clazz> clazzList = clazzService.findAll();
        return Result.success(clazzList);
    }

    /**
     * 班级列表查询
     */
    @GetMapping
    public Result page(ClazzQueryParam clazzQueryParam) {
        log.info("查询请求参数： {}", clazzQueryParam);
        PageResult pageResult = clazzService.page(clazzQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 删除班级
     */
    @LogOperation
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        if (clazzService.hasStudent(id)) {
            return Result.error("该班级有学生，不能删除");
        }
        clazzService.deleteById(id);
        log.info("删除班级");
        return Result.success();

    }

    /**
     * 修改班级信息
     */
    @LogOperation
    @PutMapping
    public Result update(@RequestBody Clazz clazz) {
        log.info("修改班级");
        clazzService.update(clazz);
        return Result.success();
    }
}
